package microservices.spring_cloud_server.rest;

import java.util.List;

import javax.ws.rs.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import microservices.spring_cloud_server.dao.RoleDao;
import microservices.spring_cloud_server.vo.PageVO;
import microservices.spring_cloud_server.vo.PagedResult;
import microservices.spring_cloud_server.vo.RoleVO;

@RestController
public class RoleService {
	@Autowired
	RoleDao roleDao;

	@RequestMapping(value = "/rolequery/{pageSize}/{pageNo}", method = RequestMethod.POST)
	public PagedResult<RoleVO> queryUserPagedList(@RequestBody RoleVO roleVO,
			@PathParam(value = "pageVO") PageVO pageVO) {
		String creationDateStr = roleVO.getCreationDate();
		String creationDateStr1 = null;
		String creationDateStr2 = null;
		if (creationDateStr.length() > 0) {
			if (creationDateStr.split("T")[1].equals("16:00:00.000Z")) {
				creationDateStr1 = Integer.valueOf(creationDateStr.split("T")[0].split("-")[2]) + 1 + "";
				if (Integer.valueOf(creationDateStr.split("T")[0].split("-")[2]) < 10) {
					creationDateStr2 = creationDateStr.split("T")[0].split("-")[0] + "-"
							+ creationDateStr.split("T")[0].split("-")[1] + "-" + "0" + creationDateStr1;
				} else {
					creationDateStr2 = creationDateStr.split("T")[0].split("-")[0] + "-"
							+ creationDateStr.split("T")[0].split("-")[1] + "-" + creationDateStr1;
				}
			} else {
				creationDateStr2 = creationDateStr.split("T")[0];
			}
			roleVO.setCreationDate(creationDateStr2);
		}
		return roleDao.queryRolePagedList(roleVO, pageVO);
	}

	@RequestMapping(value = "/roleadd", method = RequestMethod.POST)
	public void insertRole(@RequestBody RoleVO roleVO) {
		roleDao.addRole(roleVO);
	}

	@RequestMapping(value = "/roleupdate", method = RequestMethod.POST)
	public void updateRole(@RequestBody RoleVO roleVO) {
		roleDao.updateRole(roleVO);
	}

	@RequestMapping(value = "/roledelete", method = RequestMethod.POST)
	public void batchDeleteRole(@RequestBody List<RoleVO> roleList) {
		roleDao.batchDeleteRole(roleList);
	}

	@RequestMapping(value = "/getrolegroup", method = RequestMethod.POST)
	public List<RoleVO> getRole(@RequestBody RoleVO role) {
		List<RoleVO> roleGroupList = roleDao.queryRoleGroupList();
		for (RoleVO roleVO : roleGroupList) {
			RoleVO qureyVO = new RoleVO();
			qureyVO.setUserId(role.getUserId());
			qureyVO.setRoleGroup(roleVO.getTitle());
			roleVO.setChildren(roleDao.queryChildRoleList(qureyVO));
		}
		return roleGroupList;
	}

	@RequestMapping(value = "/userRoleInsert/{userId}", method = RequestMethod.POST)
	public void userRoleInsert(@PathVariable String userId, @RequestBody List<RoleVO> list) {
		RoleVO roleVO = new RoleVO();
		roleVO.setUserId(userId);
		roleDao.userRoleDelete(roleVO);
		if (list != null && list.size() > 0) {
			roleDao.userRoleInsert(list);
		}
	}
}
